<?php
/**
 * 
 * Класс работы с шаблоном
 * @author mr.Pie
 *
 */
class Template extends BasicModel {
	/** Список css файлов */
	private $css_list;
	/** Список js файлов */
	private $js_list;
	/** Header */
	private $header;
	/** Элементы страницы */
	private $page_elem;
	/** */
	private $page;
	private $crumbs;
  
  /** 
   * Инициализация
   */
  function __construct(Core $Site) {
		parent::__construct($Site);
		$this->crumbs = NULL;
		$this->Clear();
  }
	function Clear() {
  	$this->css_list = array();
  	$this->js_list  = array();
  	$this->header   = array(
			'title' => ''
		);
		$this->page_elem = array();
		$this->page      = array();
		return 0;
	}
	function setTitle($Title) {$this->header['title'] = $Title;}
	function setLoadPage($page) {$this->page['loadPage'] = $page;}
	function setPageElements($elem_arr) {
		if (is_array($elem_arr)) {
			foreach ($elem_arr as $elem => $text) {
				$this->page_elem[$elem] = $text;
			}
		}
	}
	function setPageElement($elem, $text) {
		$this->page_elem[$elem] = $text;
	}
	function getPageElement($elem) {
		if (isset($this->page_elem[$elem])) {
			return $this->page_elem[$elem];
		}
		return NULL;
	}
	function pageRender() {
		header('Content-type: text/html; charset=UTF-8');
		if (isset($this->page['loadPage'])) {
			require TEMPLATE_PATH.'pages/'.$this->page['loadPage'].'.php';
		} else {
			$this->Site->go404();
		}
		return 0;
	}
	function includeHeader() {
		if (TEMPLATE_PATH.'header.php') {
			require TEMPLATE_PATH.'header.php';
		} else {echo 'HEADER Not found!<br />';}
		return 0;
	}
	function includeFooter() {
		if (TEMPLATE_PATH.'footer.php') {
			require TEMPLATE_PATH.'footer.php';
		} else {echo '<br />FOOTER Not found!<br />';}
		return 0;
	}
	function renderHead() {
		echo '	<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
				<title>',$this->header['title'],'</title>';
		// css файлы
		if (is_array($this->css_list) && isset($this->css_list[0])) {
			$file = end($this->css_list);
			do {
				echo '	<link rel="stylesheet" type="text/css" href="',TEMPLATE_URL,'css/'.$file.'">';	
				$file = prev($this->css_list);
			} while($file);
		}
		// js файлы
		if (is_array($this->js_list) && isset($this->js_list[0])) {
			$file = end($this->js_list);
			do {
				echo '	<script type="text/javascript" src="'.TEMPLATE_URL.'java/'.$file.'"></script>';
				$file = prev($this->js_list);
			} while($file);
		}
		return 0;
	}
	function addCrumbs($Crumb) {$this->crumbs = $Crumb;}
	function addCSSArray($CSSArr) {foreach ($CSSArr as $CSS) {$this->css_list[] = $CSS;}}
	function addCSS($css) {$this->css_list[] = $css;}
	function addJSArray($JSArr, $Header = FALSE) {foreach ($JSArr as $JS) {$this->js_list[] = $JS;}}
	function addJS($js) {$this->js_list[] = $js;}
}
?>